package com.ly.mpayment.persist.common;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import com.ly.mpayment.core.util.Config;
import com.ly.mpayment.core.util.ResultMessage;

public class LoginFilter implements Filter {

	
	public void destroy() {

	}

	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		String requestUrl = request.getRequestURL().toString();
		 
		String sessionId = null;
		//除 密码 找回 注册 登录 页面  404 500 页面之外 所有的都需要拦截 处理 分析是否已经登录
		int tag = 0;
		if (requestUrl.contains("sign-up.html") || requestUrl.contains("home.html") || requestUrl.contains("login")
				|| requestUrl.contains("getCode") || requestUrl.contains("recovery") || requestUrl.contains("register")
				|| requestUrl.contains("404.html") || requestUrl.contains("500.html")) {
			tag = 1;
		}else if (requestUrl.contains("/html/")) {
			tag = 2;
		}else if (requestUrl.contains("/client/")) {
			tag = 1;
		}
		switch (tag) {
		
		case 0:
			//post请求检查 因返回结果不同 需要分开处理
			Cookie[] cook = request.getCookies();
			if (cook != null) {
				for (int j = 0; j < cook.length; j++) {
					if (cook[j].getName().equals("JSESSIONID")) {
						sessionId = cook[j].getValue();
						break;
					}
				}
				 
				if(sessionId != null){
					HttpSession session = SessionContext.getInstance().getSession(sessionId);
					if (session == null) {
						//没有登录 跳转到登录页面
						sendPostResultResponse(response,ResultMessage.getFailt(ResultMessage.sessionError));
					} else {
						SessionContext.getInstance().addSession(session);
						chain.doFilter(request, response);
					}
				}else{
					sendPostResultResponse(response,ResultMessage.getFailt(ResultMessage.sessionError));
				}
			} else {
				sendPostResultResponse(response,ResultMessage.getFailt(ResultMessage.sessionError));
			}
			break;
		case 1:
			chain.doFilter(request, response);
			break;
			//网页刷新请求检查
		case 2:
			cook = request.getCookies();
			if (cook != null) {
				for (int j = 0; j < cook.length; j++) {
					if (cook[j].getName().equals("JSESSIONID")) {
						sessionId = cook[j].getValue();
						break;
					}
				}
				if(sessionId != null){
					HttpSession session = SessionContext.getInstance().getSession(sessionId);
					if (session == null) {
						//没有登录 跳转到登录页面
						sendResponse(response,Config.login);
					} else {
						SessionContext.getInstance().addSession(session);
						chain.doFilter(request, response);
					}
				}else{
					sendResponse(response, Config.login);
				}
				
			} else {
				sendResponse(response, Config.notFind404);
			}
			break;
		default:
			sendResponse(response, Config.notFind404);
			break;
		}
	}

	/**
	 * 重定向页面
	 * @param response
	 * @param jo
	 */
	public void sendResponse(HttpServletResponse response, String page) {
		response.setContentType("text/html" + ";charset=UTF-8");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		try {
			response.sendRedirect(page);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 *  session 检查 跳转到login
	 * @param response
	 * @param jo
	 */
	public void sendPostResultResponse(HttpServletResponse response,String msg) {
		response.setContentType("text/html" + ";charset=UTF-8");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		try {
			response.getOutputStream().write(msg.getBytes());
			response.getOutputStream().flush();
			response.getOutputStream().close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	public void init(FilterConfig arg0) throws ServletException {

	}

}
